<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!-- 表单数据的校验 -->
      <!-- el-form 绑定 :model 属性 对象 表单提交的数据 -->
      <!-- el-form 绑定 :rules 属性 对象 表单数据校验的规则 -->
      <!-- el-form-item 绑定 prop属性 属性值 是 model对象里面的属性值 -->
      <!-- el-input 等等 绑定 v-model属性 实现双向数据绑定 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" type="password" show-password />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login_btn"
            @click="onLogin"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { REMEMBER_KEY } from '@/constants/KEY'
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: 'admin',
        password: 'admin123'
      },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      rememberMe: false
    }
  },
  created() {
    // 从本地存储里面 将数据取出来
    const formData = JSON.parse(localStorage.getItem(REMEMBER_KEY))
    // 判断 formData 有没有
    if (formData) {
      this.form.username = formData.username
      this.form.password = formData.password
    }
  },
  methods: {
    // 将 mapActions 里面的 函数映射为当前组件的函数
    ...mapActions('user', ['doLogin']),
    async onLogin() {
      // 表单的兜底校验
      // console.log()
      try {
        await this.$refs.form.validate()
        // 发送登录请求
        await this.doLogin(this.form)
        // 记住我功能
        if (this.rememberMe) {
          // 勾选了 记住我 保存输入的账号和密码
          localStorage.setItem(REMEMBER_KEY, JSON.stringify(this.form))
        } else {
          // 没有勾选 记住我 将本地存储的数据删除
          localStorage.removeItem(REMEMBER_KEY)
        }
        // 登录成功的提示
        this.$message.success('登录成功')
        // 登录成功后跳转到后台首页
        const path = this.$route.query.redirect
        if (path) {
          this.$router.push(path)
        } else {
          this.$router.push('/')
        }
      } catch (error) {
        // console.dir(error)
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
}
.bg {
  width: 60vw;
  height: 100vh;
  background: url("~@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}
.box {
  margin: 200px 10% 0;
  flex: 1;
  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }
    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }
    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}
.login_btn {
  width: 100%;
}
</style>
